﻿using System.Data;
using System.Data.SqlClient;
using KZ.Exception;
using KZ.Utilities;
using Process.Entities;
using System.Collections.Generic;

namespace Process.DAL
{
    public class PartnerDAL
    {
        private static PartnerENT CreatePartnerFromReader(IDataReader reader)
        {
            PartnerENT item = new PartnerENT();

            if (!reader.IsDBNull(reader.GetOrdinal(PartnerColumns.PartnerID.ToString()))) item.PartnerID = Utility.StringParse(reader[PartnerColumns.PartnerID.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(PartnerColumns.PartnerUsername.ToString()))) item.PartnerUsername = Utility.StringParse(reader[PartnerColumns.PartnerUsername.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(PartnerColumns.PartnerPassword.ToString()))) item.PartnerPassword = Utility.StringParse(reader[PartnerColumns.PartnerPassword.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(PartnerColumns.BusinessUsername.ToString()))) item.BusinessUsername = Utility.StringParse(reader[PartnerColumns.BusinessUsername.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(PartnerColumns.BusinessPassword.ToString()))) item.BusinessPassword = Utility.StringParse(reader[PartnerColumns.BusinessPassword.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(PartnerColumns.PartnerName.ToString()))) item.PartnerName = Utility.StringParse(reader[PartnerColumns.PartnerName.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(PartnerColumns.PartnerContactPerson.ToString()))) item.PartnerContactPerson = Utility.StringParse(reader[PartnerColumns.PartnerContactPerson.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(PartnerColumns.PartnerAddress.ToString()))) item.PartnerAddress = Utility.StringParse(reader[PartnerColumns.PartnerAddress.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(PartnerColumns.PartnerPhone.ToString()))) item.PartnerPhone = Utility.StringParse(reader[PartnerColumns.PartnerPhone.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(PartnerColumns.PartnerPaymentCard.ToString()))) item.PartnerPaymentCard = Utility.StringParse(reader[PartnerColumns.PartnerPaymentCard.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(PartnerColumns.WS1.ToString()))) item.WS1 = Utility.StringParse(reader[PartnerColumns.WS1.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(PartnerColumns.WS2.ToString()))) item.WS2 = Utility.StringParse(reader[PartnerColumns.WS2.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(PartnerColumns.WS3.ToString()))) item.WS3 = Utility.StringParse(reader[PartnerColumns.WS3.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(PartnerColumns.WS4.ToString()))) item.WS4 = Utility.StringParse(reader[PartnerColumns.WS4.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(PartnerColumns.WS5.ToString()))) item.WS5 = Utility.StringParse(reader[PartnerColumns.WS5.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(PartnerColumns.IsDeleted.ToString()))) item.IsDeleted = Utility.BoolParse(reader[PartnerColumns.IsDeleted.ToString()]);

            return item;
        }

        public static PartnerENT GetPartnerByUsername(string username)
        {
            try
            {
                PartnerENT partner = new PartnerENT();

                DataAccessHelper.AddParameter("Username", SqlDbType.VarChar, username);

                using (IDataReader reader = DataAccessHelper.ExecuteQuery("procPartnerGetByUsername"))
                {
                    while (reader.Read())
                    {
                        partner = CreatePartnerFromReader(reader);
                    }
                }

                return partner;
            }
            catch (SqlException)
            {
                throw new DataAccessException("Có lỗi trong quá trình lấy thông tin nhà kinh doanh.");
            }
            finally
            {
                DataAccessHelper.ClearCommand();
            }
        }

        public static PartnerENT GetPartnerByID(string partnerID)
        {
            try
            {
                PartnerENT partner = new PartnerENT();

                DataAccessHelper.AddParameter("PartnerID", SqlDbType.VarChar, partnerID);

                using (IDataReader reader = DataAccessHelper.ExecuteQuery("procPartnerGetByID"))
                {
                    while (reader.Read())
                    {
                        partner = CreatePartnerFromReader(reader);
                    }
                }

                return partner;
            }
            catch (SqlException)
            {
                throw new DataAccessException("Có lỗi trong quá trình lấy thông tin nhà kinh doanh.");
            }
            finally
            {
                DataAccessHelper.ClearCommand();
            }
        }

        public static List<PartnerENT> GetPartnerList()
        {
            try
            {
                List<PartnerENT> providerList = new List<PartnerENT>();

                using (IDataReader reader = DataAccessHelper.ExecuteQuery("procPartnerGetList"))
                {
                    while (reader.Read())
                    {
                        providerList.Add(CreatePartnerFromReader(reader));
                    }
                }

                return providerList;
            }
            catch (SqlException)
            {
                throw new DataAccessException("Có lỗi trong quá trình lấy thông tin nhà kinh doanh.");
            }
            finally
            {
                DataAccessHelper.ClearCommand();
            }
        }
    }
}